/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.user.vip.web;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField.Type;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.app.util.time.DateUtils;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.zx.question.bank.entity.TquestionBank;
import com.jeesite.modules.zx.user.vip.entity.TuserVip;
import com.jeesite.modules.zx.user.vip.service.TuserVipService;

/**
 * 用户会员管理Controller
 * @author zpp
 * @version 2020-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/user/vip/tuserVip")
public class TuserVipController extends BaseController {

	@Autowired
	private TuserVipService tuserVipService;
	@Autowired
	private EmpUserService empUserService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TuserVip get(String id, boolean isNewRecord) {
		return tuserVipService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:user:vip:tuserVip:view")
	@RequestMapping(value = {"list", ""})
	public String list(TuserVip tuserVip, Model model) {
		model.addAttribute("tuserVip", tuserVip);
		return "modules/zx/user/vip/tuserVipList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:user:vip:tuserVip:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TuserVip> listData(TuserVip tuserVip, HttpServletRequest request, HttpServletResponse response) {
		tuserVip.setPage(new Page<>(request, response));
		Page<TuserVip> page = tuserVipService.findPage(tuserVip);
		List<TuserVip> list = page.getList();
		for(TuserVip tv:list){
			String userId = tv.getUserId();
			if(StringUtils.isNotBlank(userId)){
				User user = UserUtils.get(userId);
				if(user!=null){
				    tv.setUserName(user.getUserName());
				}
			}
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:user:vip:tuserVip:view")
	@RequestMapping(value = "form")
	public String form(TuserVip tuserVip, Model model) {
		model.addAttribute("tuserVip", tuserVip);
		EmpUser empUser=new EmpUser();
		List<EmpUser> findList = empUserService.findList(empUser);
		model.addAttribute("userList", findList);
		return "modules/zx/user/vip/tuserVipForm";
	}

	/**
	 * 保存用户会员管理
	 */
	@RequiresPermissions("zx:user:vip:tuserVip:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TuserVip tuserVip) {
		
		Date vipStarttime = tuserVip.getVipStarttime();
		LocalDateTime date2LocalDateTime = DateUtils.date2LocalDateTime(vipStarttime);
		Date vipEndtime = tuserVip.getVipEndtime();
		LocalDateTime date2LocalDateTime2 = DateUtils.date2LocalDateTime(vipEndtime);
		Long betweenDays = DateUtils.betweenDays(date2LocalDateTime,date2LocalDateTime2);
		if(betweenDays<=0){
			return renderResult(Global.FALSE, text("会员有效期：开始时间必须大于结束时间"));
		}
		tuserVipService.save(tuserVip);
		return renderResult(Global.TRUE, text("保存用户会员管理成功！"));
	}
	
	/**
	 * 停用用户会员管理
	 */
	@RequiresPermissions("zx:user:vip:tuserVip:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TuserVip tuserVip) {
		tuserVip.setStatus(TuserVip.STATUS_DISABLE);
		tuserVipService.updateStatus(tuserVip);
		return renderResult(Global.TRUE, text("停用用户会员管理成功"));
	}
	
	/**
	 * 启用用户会员管理
	 */
	@RequiresPermissions("zx:user:vip:tuserVip:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TuserVip tuserVip) {
		tuserVip.setStatus(TuserVip.STATUS_NORMAL);
		tuserVipService.updateStatus(tuserVip);
		return renderResult(Global.TRUE, text("启用用户会员管理成功"));
	}
	
	/**
	 * 删除用户会员管理
	 */
	@RequiresPermissions("zx:user:vip:tuserVip:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TuserVip tuserVip) {
		tuserVipService.delete(tuserVip);
		return renderResult(Global.TRUE, text("删除用户会员管理成功！"));
	}
	//
	@RequestMapping(value = "importTemplate")
	public void importTemplate(HttpServletRequest request,HttpServletResponse response) {
		TuserVip tuserVip=new TuserVip();
		tuserVip.setPage(new Page<>(request, response));
		Page<TuserVip> findPage = tuserVipService.findPage(tuserVip);
		List<TuserVip> list = findPage.getList();
		for(TuserVip usVip:list){
			String userId = usVip.getUserId();
			User user = UserUtils.get(userId);
			usVip.setUserName(user.getUserName());
			usVip.setMobile(user.getMobile());
		}
		String fileName = "用户VIP数据模板.xlsx";
		try(ExcelExport ee = new ExcelExport("用户VIP数据", TuserVip.class, Type.IMPORT)){
			ee.setDataList(list).write(response, fileName);
		}
	}
	/**
	 * 导入用户数据
	 */
	@ResponseBody
	@RequiresPermissions("zx:question:bank:tquestionBank:edit")
	@PostMapping(value = "importData")
	public String importData(MultipartFile file, String updateSupport) {
		try {
			boolean isUpdateSupport = Global.YES.equals(updateSupport);
			String message = tuserVipService.importData(file, isUpdateSupport);
			return renderResult(Global.TRUE, "posfull:"+message);
		} catch (Exception ex) {
			return renderResult(Global.FALSE, "posfull:"+ex.getMessage());
		}
	}
	/**
	 * 导出用户数据
	 */
	@RequestMapping(value = "exportData")
	public void exportData(TuserVip entity, Boolean isAll, String ctrlPermi, HttpServletResponse response) {
		List<TuserVip> findList = tuserVipService.findList(entity);
		for(TuserVip uvip:findList){
			String userId = uvip.getUserId();
			User user = UserUtils.get(userId);
			uvip.setUserName(user.getUserName());
			uvip.setMobile(user.getMobile());
		}
		String fileName = "用户VIP数据" + com.jeesite.common.lang.DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try(ExcelExport ee = new ExcelExport("用户VIP数据", TuserVip.class)){
			ee.setDataList(findList).write(response, fileName);
		}
	}
}